package com.ymh.spring.learn.sw.demo1;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * 
 * 名称：AccountServiceImpl<br/>
 * 描述：转账案例业务层实现类<br/>
 * 创建时间：2018年3月14日 下午3:34:22 <br/>
 * 
 * @author aivin
 * @version 1.0.0
 */
public class AccountServiceImpl implements IAccountService {

	/**
	 * 注入Dao层类
	 */
	private IAccountDao accountDao;

	public void setAccountDao(IAccountDao accountDao) {
		this.accountDao = accountDao;
	}

	/**
	 * 注入事务管理的模板类
	 */
	private TransactionTemplate transactionTemplate;

	public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
		this.transactionTemplate = transactionTemplate;
	}

	/**
	 * 转账方法
	 */
	public void transafer(final String outAccount, final String inAccount,
			final Double money) {
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {

			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				accountDao.outMoney(outAccount, money);
//				int a = 1 / 0;
				accountDao.inMoney(inAccount, money);
			}
		});
	}
}
